package main;

import java.awt.Dimension;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;

import javax.swing.JOptionPane;

import model.GameModel;
import util.LevelUtils;
import view.test.GameBoardPanel;
import view.test.GlassPane;
import view.test.MainFrame;
import view.test.MainPanel;
import view.test.ViewBin;
import controller.GameController;

public class Main {
	
	public static void main(String[] args) {
		
		init();
			
	}
	
	public static void init() {
	    try {
	        javax.swing.SwingUtilities.invokeLater(new Runnable() {
	            public void run() {
	                try {
						createGUI();
					} catch (URISyntaxException e) {
						e.printStackTrace();
					}
	            }
	        });
	    } catch (Exception e) {
	    	
	        System.err.println("createGUI didn't successfully complete");
	       e.printStackTrace();
	    }
	}

	private static void createGUI() throws URISyntaxException {
		
		GameModel model = new GameModel();

		MainFrame frame = new MainFrame();
		MainPanel mainPanel = new MainPanel();
		
		GlassPane glassPane = new GlassPane(new Dimension(820, 620));
		GameController controller = new GameController(model, mainPanel);
		GameBoardPanel panel = new GameBoardPanel(model, controller, glassPane);
		ViewBin viewBin = new ViewBin(controller, model, glassPane);
		
		mainPanel.setController(controller);
		mainPanel.setGameBoardPanel(panel);
		mainPanel.setViewBin(viewBin);
		mainPanel.setGlassPane(glassPane);
		
		frame.add(mainPanel);
		frame.pack();
		
		model.registerWithModel(panel);
		model.createNewGameBoard();
		model.createNewBin();
		
		LevelUtils utils = new LevelUtils();
		try {
			utils.parseLevel(utils.getLevelNames()[0], model);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(mainPanel, "Error Loading Level File");
		}
		
		String[] levelNameList = null;
		try {
			levelNameList = utils.getLevelNames();
		} catch (IOException e) {

			JOptionPane.showMessageDialog(mainPanel, "Error Loading Level List File");
					
		}
		mainPanel.setLevelNameList(levelNameList);
		
		model.setDirty();
	}
	
}
